package cn.shujuhai.common.controller.setting;

import cn.shujuhai.common.domain.PageRequest;
import cn.shujuhai.common.domain.Setting;
import cn.shujuhai.common.pluging.data.ResultCode;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.common.service.SettingService;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setting")
@RequiredArgsConstructor
public class SettingController {

    final SettingService settingService;

    @PostMapping("/save")
    public ResultVo addSetting(@RequestBody @Validated Setting setting) {
        boolean code = this.settingService.insertSelective(setting);
        if (code) {
            return ResultVo.success("添加全局设置成功");
        } else return ResultVo.failure(ResultCode.FAILED, "添加全局设置失败");
    }

    @PostMapping("/update")
    public ResultVo updateSetting(@RequestBody @Validated Setting setting) {
        boolean code = this.settingService.updateSelective(setting);
        if (code) {
            return ResultVo.success("修改全局设置成功");
        } else return ResultVo.failure(ResultCode.FAILED, "修改全局设置失败");
    }

    @PostMapping("/delete/{id}")
    public ResultVo deleteSetting(@PathVariable Long id) {
        int code = this.settingService.delById(id);
        if (code > 0) {
            return ResultVo.success("删除" + code + "个全局设置成功");
        } else return ResultVo.failure(ResultCode.FAILED, "删除全局设置失败");
    }

    @PostMapping("/batchdelete")
    public ResultVo deleteSetting(@RequestParam List<Long> ids) {
        int code = this.settingService.batchdelete(ids);
        if (code > 0) {
            return ResultVo.success("删除" + code + "个全局设置成功");
        } else return ResultVo.failure(ResultCode.FAILED, "删除全局设置失败");
    }

    @GetMapping("/select/{settingId}")
    public ResultVo<Setting> selectById(@PathVariable Long settingId) {
        Setting setting = this.settingService.selectById(settingId);
        return ResultVo.success(setting);
    }

    @GetMapping("/list")
    public ResultVo<PageInfo<Setting>> getSettingsByName(
            @RequestParam(required = false) @Validated String name,
            @Valid PageRequest pageRequest) {
        PageInfo<Setting> pageInfo = this.settingService.getPagedSettingsByName(name, pageRequest.getPageNum(), pageRequest.getPageSize());
        return ResultVo.success(pageInfo); // 将 PageInfo 封装在 ResultVo 中返回
    }

    /**
     * 获取网站版权信息
     *
     * @return 响应结果
     */
    @GetMapping("/copyright")
    public ResponseEntity<Map<String, String>> getWebCopyright() {
        Map<String, String> response = new HashMap<>();
        String webCopyright = settingService.getWebCopyright();
        response.put("webCopyright", webCopyright);
        return ResponseEntity.ok(response);
    }

    /**
     * 获取全局参数
     *
     * @return 成功或失败的响应状态及结果
     */
    @GetMapping("/getSettingValue")
    public ResultVo<?> getSettingValue(String settingKey) {
        return this.settingService.getSettingValue(settingKey);
    }
}

